package com.thinkfit.jy.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.lib.bean.UserBean;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.thinkfit.jy.R;
import com.thinkfit.jy.contract.UpdateProfileContract;
import com.thinkfit.jy.dialog.UserPopupWindow;
import com.thinkfit.jy.presenter.UpdateProfilePresenter;
import com.thinkfit.jy.wheelview.WheelItem;
import com.thinkfit.jy.wheelview.WheelView;

/**
 * 设置用户身高弹窗界面
 */
public class ChooseHeightFragment extends BaseFragment<UpdateProfileContract.Presenter> implements UpdateProfileContract.View {


    private int heightStart = 100;//设置身高的起始值
    private int heightEnd = 220;//设置身高的结束值

    @NonNull
    @Override
    protected UpdateProfileContract.Presenter onCreatePresenter() {
        return new UpdateProfilePresenter(this);
    }


    @Override
    protected int getLayoutId() {
//        return DataManager.Companion.getInstance().getMainStyle() == 3 ?
//                R.layout.fragment_choose_height_xm :
        return      R.layout.fragment_choose_height;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        setViewsOnClickListener(R.id.tv_ok);
        WheelView wheelView = view.findViewById(R.id.wheelView);
        WheelItem[] items = new WheelItem[heightEnd-heightStart+1];
        for (int i = heightStart; i <= heightEnd; ++i) {
            items[i - heightStart] = new WheelItem(String.valueOf(i));
        }
        wheelView.setItems(items);
        UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
        wheelView.setSelectedIndex(myInfo.getHeight() - heightStart);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_ok:
                UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
                WheelView wheelView = getView().findViewById(R.id.wheelView);
                if (myInfo.getHeight() == wheelView.getSelectedIndex() + heightStart) {
                    finish();
                    return;
                }
                UserPopupWindow.OnUserOlick onUserOlick = new UserPopupWindow.OnUserOlick() {
                    @Override
                    public void onClick() {
                        getPresenter().updateUser("height", wheelView.getSelectedIndex() + heightStart);
                    }
                };
                UserPopupWindow userPopupWindow = new UserPopupWindow(getContext(), onUserOlick);
                userPopupWindow.show(getActivity());
                break;
        }
    }

    @Override
    public void updateUserSuccess(Object height) {
        if (getView() == null) {
            return;
        }
        UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
        myInfo.setHeight((int) height);
        DataManager.Companion.getInstance().saveMyInfo(myInfo);
        finish();
    }
}
